package com.middle;

import com.xgh.TreeNode;

import java.util.*;

/**
 * @ClassName Test199
 * @Description
 * @Author xinggh
 * @Date 2020/8/29 23:12
 * @Version 1.0
 **/
public class Test199 {

    /**
     * 199. 二叉树的右视图
     * 给定一棵二叉树，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
     * @param root
     * @return
     */
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();

        if (root == null) {
            return result;
        }

        Deque<TreeNode> deque = new LinkedList();
        deque.add(root);
        while (!deque.isEmpty()) {

            List<Integer> list = new ArrayList<>();
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode pop = deque.pop();
                if (pop.left != null) {
                    deque.add(pop.left);

                }
                if (pop.right != null) {
                    deque.add(pop.right);

                }
                if (i == (size - 1)) {
                    result.add(pop.val);
                }
            }

        }

        return result;
    }
}
